import com.ailk.kbi.kbirazor.component.NodeGroup;
import com.ailk.kbi.kbirazor.control.KbrControl;
import com.ailk.kbi.kbirazor.main.model.MainModel;
import com.ailk.kbi.kbirazor.model.AreaModel;
import com.ailk.kbi.kbirazor.model.BrandModel;
import com.ailk.kbi.kbirazor.model.KbrModel;
import com.ailk.kbi.kbirazor.model.TimeModel;
import com.ailk.kbi.kbirazor.view.AreaWindow;
import com.ailk.kbi.kbirazor.view.BrandWindow;
import com.ailk.kbi.kbirazor.view.ResizableTitleWindow;
import com.ailk.kbi.kbirazor.view.ResizableTitleWindowSkin;
import com.ailk.kbi.kbirazor.view.TimeWindow;
import com.ailk.kbi.kbirazor.vo.KpiVO;
import com.ailk.kbi.kbirazor.vo.MindMapVO;
import com.ailk.kbi.kbirazor.vo.UserVO;

import flash.events.Event;
import flash.events.MouseEvent;
import flash.system.Security;

import mx.charts.Legend;
import mx.charts.LineChart;
import mx.collections.XMLListCollection;
import mx.controls.AdvancedDataGrid;
import mx.controls.Alert;
import mx.controls.advancedDataGridClasses.AdvancedDataGridColumn;
import mx.core.IFlexDisplayObject;
import mx.events.DragEvent;
import mx.managers.DragManager;
import mx.managers.PopUpManager;

import spark.components.TitleWindow;




[Bindable]
private var kbrModel:KbrModel = KbrModel.getInstance();

[Bindable]
private var areaModel:AreaModel = AreaModel.getInstance();

[Bindable]
private var timeModel:TimeModel = TimeModel.getInstance();

[Bindable]
private var brandModel:BrandModel = BrandModel.getInstance();

[Bindable]
private var mainModel:MainModel = MainModel.getInstance();


private var kbrControl:KbrControl = new KbrControl();
private function initApp():void
{
	Security.allowDomain("*");
	/**********权限模拟************/
	var userVO:UserVO = new UserVO();
	userVO.userId = "1";
	userVO.cityId = "999";
	userVO.areaId = "999";
	mainModel.userVO = userVO;
	
	/**********权限模拟************/
	
	
	kbrModel.addEventListener("anchorChange", anchorChangeHandler);
	
	/*脑图XML变更事件*/
	kbrModel.addEventListener("mindMapXMLChange", mindMapXMLChangeHandler);
	
	/*时间范围变更事件*/
	kbrModel.addEventListener("selectableRangeChange", selectableRangeChangeHandler);
	
	kbrModel.addEventListener("kpiValuesChange", kpiValuesChangeHandler);
	
	kbrControl.getMindMapList();
	
	
}

private var _mainNodeGroup:NodeGroup;

private function anchorChangeHandler(event:Event):void{
	if(_mainNodeGroup){
		_mainNodeGroup.removeEventListener("widthChanged", widthChangedHandler);
		_mainNodeGroup.removeEventListener("heightChanged", heightChangedHandler);
	}
	mainGroup.removeAllElements();
	_mainNodeGroup = new NodeGroup(XML(kbrModel.mindMapXML.node));
	mainGroup.addElement(_mainNodeGroup);	
	_mainNodeGroup.x = 10;
	_mainNodeGroup.y = 30;
	_mainNodeGroup.addEventListener("widthChanged", widthChangedHandler);
	_mainNodeGroup.addEventListener("heightChanged", heightChangedHandler);
}

private function widthChangedHandler(event:Event):void {
	mainGroup.width = _mainNodeGroup.width + 20;
}

private function heightChangedHandler(event:Event):void {
	mainGroup.height = _mainNodeGroup.height + 40;
}


/**
 * 处理脑图XML变更事件
 */
private function mindMapXMLChangeHandler(event:Event):void{
	anchorChangeHandler(null);
	if(kbrModel.timeSQL == null){
		Alert.show("脑图未定义时间范围！");
	}
	else{
		kbrControl.getTimeRangeVO();
	}
}

private function kpiValuesChangeHandler(event:Event):void{
	//	Alert.show("kpiValuesChangeHandler");
	anchorChangeHandler(null);
}

/**日期选择范围变更事件*/
private function selectableRangeChangeHandler(event:Event):void{
	dfOpTime.selectableRange = kbrModel.selectableRange;
	dfOpTime.selectedDate = dfOpTime.selectableRange.rangeEnd;
	opTimeChangeHandler();
}

private function opTimeChangeHandler():void{
	kbrControl.getKpiValues();
	//	dfOpTime.selectedDate;
}

private function changeMM(e:Event):void {
	if(e.currentTarget.selectedItem){
		kbrModel.mindMapVO = e.currentTarget.selectedItem as MindMapVO;
		kbrControl.loadMindMapXML();
	}
	
}

private function cmdAreaAnalyse_clickHandler(event:MouseEvent):void
{
	if(areaModel.windowFlag) {
		
	} else {
		var areaWindow:AreaWindow = PopUpManager.createPopUp(this, AreaWindow, false) as AreaWindow;
		PopUpManager.centerPopUp(areaWindow);
		
		areaModel.windowFlag = true;
	}
	
}
private function cmdTimeAnalyse_clickHandler(event:MouseEvent):void
{
	if(timeModel.windowFlag){
		
	} else {
		var timeWindow:TimeWindow = PopUpManager.createPopUp(this, TimeWindow, false) as TimeWindow;
		PopUpManager.centerPopUp(timeWindow);
		
		timeModel.windowFlag = true;
	}
	
}
private function cmdBrandAnalyse_clickHandler(event:MouseEvent):void
{
	if(brandModel.windowFlag){
		
	} else {
		var brandWindow:BrandWindow = PopUpManager.createPopUp(this, BrandWindow, false) as BrandWindow;
		PopUpManager.centerPopUp(brandWindow);
		
		brandModel.windowFlag = true;
	}
	
}

private function mainGroup_clickHandler(event:MouseEvent):void
{
	if(event.target==mainGroup && kbrModel.nodeSelectedFlag) {
		kbrModel.clickFocus();	
	} else {
		//					Alert.show("none");
	}
}